package com.ldl.customize.common;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.HashMap;

/**
 * 功能描述:启动时加载一些东西
 * 作者: LDL
 * 创建时间: 2014/5/11 16:58
 */
public class InitServlet extends HttpServlet {

    private static final Logger logger = LoggerFactory.getLogger(InitServlet.class);
    //省级单位(代码,名称)
    private static HashMap<String, String> provincesMap;
    //省级单位代码，下级市级单位(代码，名称)
    private static HashMap<String, HashMap<String, String>> citysMap;

    @Override
    public void init() throws ServletException {
        super.init();
        logger.debug("初始化开始...");
        logger.debug("获取天气设置省级单位信息");
        getProvinces();
        logger.debug("获取天气设置市级单位信息");
        getCitys(provincesMap);
    }


    /**
     * 获取天气设置要使用的省级单位信息
     */
    public void getProvinces() {

        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod("http://www.weather.com.cn/data/list3/city.xml");
        //设置成了默认的恢复策略，在发生异常时候将自动重试3次
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        //执行Method
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            if (statusCode != HttpStatus.SC_OK) {
                logger.debug("请求失败:" + getMethod.getStatusLine());
            }
            byte[] responseBody = getMethod.getResponseBody();
            String result = new String(responseBody);
            logger.debug(result);
            String[] results = result.split(",");
            String[] provinces;

            for (String province : results) {
                provinces = province.split("\\|");
                provincesMap.put(provinces[0], provinces[1]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            getMethod.releaseConnection();
        }
    }

    /**
     * 获取市级单位信息
     */
    public void getCitys(HashMap<String, String> provincesMap) {

        HttpClient httpClient = new HttpClient();
        String value="";
        for(String key : provincesMap.keySet()){
            value= provincesMap.get(key);
            GetMethod getMethod = new GetMethod("http://www.weather.com.cn/data/list3/city" + key + ".xml");
            getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            int statusCode = 0;
            try {
                statusCode = httpClient.executeMethod(getMethod);

                if (statusCode != HttpStatus.SC_OK) {
                    logger.debug("请求失败:" + getMethod.getStatusLine());
                }
                byte[] responseBody = getMethod.getResponseBody();
                String result = new String(responseBody);
                String[] results = result.split(",");
                String[] citys;
                HashMap<String,String> cityMap = new HashMap<String,String>();
                for (String city : results) {
                    citys = city.split("\\|");
                    cityMap.put(citys[0],citys[1]);
                }
                citysMap.put(value,cityMap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
